<?php
class PointsController extends AppController {

	var $name = 'Points';
	var $uses = array(
		'Person',
		'Meal',
		'Workout',
		'Point',
		'Setting');

	function index() {
		$this->Point->recursive = 0;
		$this->set('points', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid point', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('point', $this->Point->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Point->create();
			if ($this->Point->save($this->data)) {
				$this->Session->setFlash(__('The point has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The point could not be saved. Please, try again.', true));
			}
		}
		$people = $this->Point->Person->find('list');
		$this->set(compact('people'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid point', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Point->save($this->data)) {
				$this->Session->setFlash(__('The point has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The point could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Point->read(null, $id);
		}
		$people = $this->Point->Person->find('list');
		$this->set(compact('people'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for point', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Point->delete($id)) {
			$this->Session->setFlash(__('Point deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Point was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	
	function reset($findCase = 'all', $conditions = null) {
		$pointCount = 0;
		$msg = '';
		foreach($this->Point->find($findCase,$conditions) as $p) {
			// if $p is not an array we know a list is being processed
			// set record id depending on structure
			$id = (!is_array($p)) ? $p : $p['Point']['id'];
			if($this->Point->delete($id)) {
				$pointCount++;
			}
		}
		if($pointCount) {
			$msg .= $pointCount . 'p ';
		}
		
		$workCount = 0;
		$conditions['conditions']['calculated'] = 1; //only reset records already counted
		debug($this->Workout->find($findCase,$conditions));
		foreach($this->Workout->find($findCase,$conditions) as $w) {
			$w['Workout']['calculated'] = 0;
			if($this->Workout->save($w)) {
				$workCount++;				
			}
		}
		if($workCount) {
			$msg .= $workCount . 'w ';
		}
		
		$mealCount = 0;
		foreach($this->Meal->find($findCase,$conditions) as $m) {
			$m['Meal']['calculated'] = 0;
			if($this->Meal->save($m)) {
				$mealCount++;				
			}
		}
		if($mealCount) {
			$msg .= $mealCount . 'm ';
		}
		
		$msg = ($msg) ? $msg : "Nothing to ";
		
		$this->Session->setFlash(__($msg . 'reset', true));
		$this->redirect('/');
	}
	
	function calculate($findCase = 'all', $options = null) {
			
		if (!$findCase || $findCase == 'list') {
			$this->Session->setFlash(__('Invalid find case', true));
			$this->redirect(array('action'=>'index'));
		}
		
		// only pull records that haven't been touched!
		$options['conditions']['calculated'] = 0;
		
		// both meals and exercises need to be calculated for
		$options['fields'] = 'Meal.*';
		$meals = $this->Meal->find($findCase,$options);
		
		$options['fields'] = 'Workout.*';
		$workouts = $this->Workout->find($findCase,$options);
		
		// debug(array('meals'=>$meals, 'workouts'=>$workouts));
		
		
		foreach($this->Setting->find('all') as $s)
			extract(array($s['Setting']['name'] => $s['Setting']['value']));
		
		
		// PER PERSON
			$person = $this->Person->findByName('Dave');
			
			// per day
				// calculate running totals
				
				$cal_goal 		= 1800;
				$points 		= 0;
				$calories 		= 0;
				$grams_carb 	= 0;
				$grams_fat 		= 0;
				$grams_protein 	= 0;
				
				foreach($meals as $m) {
					$calories 		+= $m['Meal']['calories'];
					$grams_carb 	+= $m['Meal']['grams_carb'];
					$grams_fat 		+= $m['Meal']['grams_fat'];
					$grams_protein 	+= $m['Meal']['grams_protein'];	
					$m['Meal']['calculated'] = 1;
					$m['Meal']['updated'] = null;
					$this->Meal->save($m);
				}
				
				// bonuses		
				if ($calories > $cal_goal * (2/3) && $calories <= $cal_goal) {
					$points += (1000-($cal_goal - $calories))/5;
				} else if ($calories > $cal_goal && $calories < $cal_goal + ($cal_goal*1/6)) {
					$points += (1000-($calories - $cal_goal)) / 7.5;
				} else if ($calories > $cal_goal + ($cal_goal*1/6)) {
					$points -= (1000-($calories - $cal_goal)) / 20;
				}	
				
				//bonus points if nutrients within target range
				$points += ($grams_carb > $CARB_RATIO_LOWER && $grams_carb < $CARB_RATIO_UPPER) ? 25 : -12 ;
				$points += ($grams_fat > $FAT_RATIO_LOWER && $grams_fat < $FAT_RATIO_UPPER) ? 15 : -8;
				$points += ($grams_protein > $PROTEIN_RATIO_LOWER && $grams_protein < $PROTEIN_RATIO_UPPER) ? 10 : -5;
				
				$totals = array(
					'points' => $points,
					'calories' => $calories,
					'cal_goal' => $cal_goal,
					'grams_carb' => $grams_carb,
					'grams_fat' => $grams_fat,
					'grams_protein' => $grams_protein,
				);
				$debug['POST MEAL TOTALS'] = $totals;
				
				
				// START EXERCISE CALCULATIONS
				$calories_burned = 0;
				$duration = 0;
				
				foreach($workouts as $w) {
					$calories_burned += $w['Workout']['calories_burned'];
					$duration += $w['Workout']['duration'];
					$w['Workout']['calculated'] = 1;
					$w['Workout']['updated'] = null;
					$this->Workout->save($w);
				}
				
				$workPoints = 0;
				$workPoints += $calories_burned;
				$workPoints *= ($duration >= $WORKOUT_MODIFIER_DURATION_MIN) ? 1.2 : 1 ;
				
				$points += $workPoints;
				
				$totals = array(
					'points' => $workPoints,
					'total' => $points,
					'calories_burned' => $calories_burned,
					'duration' => $duration,
				);
				$debug['POST EXERCISE TOTALS'] = $totals;
				
				$data['Point'] = array(
					'person_id' => $person['Person']['id'],
					'points' => $points,
					// add crated time here based on DATE of loop
				);
				if($this->Point->save($data)) {
					$this->Session->setFlash(__('Points Added', true));
					$this->redirect(array('controller' => 'people','action'=>'view',$person['Person']['id']));
				}				
			//end per day
			debug($debug);		
		// end per person
	}	
	
}
